Skip to main content

Random ID Generator

Requirements

  1. Must be unique across all node points of system
  2. Should be a fixed size (e.g. int64)
  3. Contains timestamp, easily sort data
  4. Should not have any duplicates
  5. Scalable to handle high QPS

Naive Solution

Auto Increment in DB

  • Id will be just SQL auto increment

Use current time

  • Id will base on the current time

Drawbacks: Not feasible in a distributed environment, collision will occur easily

UUID

  • Universally Unique Identifier
  • 128 bit number (takes up substantial space)
  • Extremely low chance of duplication
  • Id generated cannot be predicted in advanced

Twitter Slowflake

  • 64 bit number (Lesser storage)
  • Composed of:
    • 41 bit epoch timestamp (milliseconds precision)
    • 10 bit machine ID (up to 1024 machines)
    • 12 bit counter bits (generated by local counter uo to 4096)
    • 1 extra bit for future use. default 0
  • Sortable by time (contains time stamp)
  • Zookeeper needed to maintain the machine IDs
  • Future Ids are predictable

References: